package com.goods.controller;

import com.goods.pojo.Book;
import com.goods.pojo.Page;
import com.goods.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/BookController")
public class BookController {
	
	@Resource
	private BookService bookServiceImpl;
	
	/**
	 * 获取当前页码
	 * @param req
	 * @return
	 */
	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		if(param != null && !param.trim().isEmpty()) {
			try {
				pc = Integer.parseInt(param);
			} catch(RuntimeException e) {}
		}
		return pc;
	}
	
	// 截取url，分页导航中使用它做为超链接的目标
	/*
	 * http://localhost:8080/goods/BookController/findByCategory?cid=xxx&pc=3
	 * /goods/BookController/findByCategory + cid=xxx&pc=3
	 */
	private String getUrl(HttpServletRequest req) {
		String url = req.getRequestURI() + "?" + req.getQueryString();
		///goods/BookController/findByCategory?cid=5F79D0D246AD4216AC04E9C5FAB3199E++++++++++++++++++++++++++++
		/*如果url中存在pc参数，就截取掉，否则截取。
		* 	lastIndexOf : 从右到左，字符串出现的最后一次位置
		*  */
		int index = url.lastIndexOf("&pc=");
		if(index != -1) {
			// 截取字符串 索引 0 - index
			url = url.substring(0, index);
		}
		return url;
	}
	

	// 按分类查
	@RequestMapping(value = "/findByCategory")
	public String findByCategory(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		//得到pc：如果页面传递，使用页面的，如果没传，pc=1
		int pc = getPc(req);
		//得到url：...
		String url = getUrl(req);
		//获取分类的id
		String cid = req.getParameter("cid");
		//得到Page
		Page<Book> pb = bookServiceImpl.findByCategory(cid, pc);
		//给Page设置url，保存Page
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/jsps/book/list.jsp";
	}
	

	// 按作者查
	@RequestMapping(value = "/findByAuthor")
	public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		int pc = getPc(req);
		String url = getUrl(req);
		// 获取查询条件
		String author = req.getParameter("author");
		// 调用findByAuthor得到Page
		Page<Book> pb = bookServiceImpl.findByAuthor(author, pc);
		// 给Page设置url，保存Page，转发到/jsps/book/list.jsp
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/jsps/book/list.jsp";
	}
	
	// 按出版社查询
	@RequestMapping(value = "/findByPress")
	public String findByPress(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		int pc = getPc(req);
		String url = getUrl(req);
		String press = req.getParameter("press");
		Page<Book> pb = bookServiceImpl.findByPress(press, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/jsps/book/list.jsp";
	}

	// 按图书名查
	@RequestMapping(value = "/findByBname")
	public String findByBname(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		int pc = getPc(req);
		String url = getUrl(req);
		String bname = req.getParameter("bname");
		Page<Book> pb = bookServiceImpl.findByBname(bname, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/jsps/book/list.jsp";
	}

	//多条件组合查询
	@RequestMapping(value = "/findByCombination")
	public String findByCombination(Book book,HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		int pc = getPc(req);
		String url = getUrl(req);
		Page<Book> pb = bookServiceImpl.findByCombination(book, pc);
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/jsps/book/list.jsp";
	}
	
	
	// 查询一本书的详细信息,按bid查询
	@RequestMapping(value = "/load")
	public String load(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		String bid = req.getParameter("bid");
		Book book = bookServiceImpl.findByBid(bid);
		req.setAttribute("book", book);
		return "/jsps/book/desc.jsp";
	}
}
